<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ include file="ajax.jsp"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>欢迎访问后台管理系统</title>
    <style>
        /* 重置样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        .flex-row {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        /* 页面主体 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f5f5f5;
            display: flex;
            justify-content: center;
            /*align-items: center;*/
            height: 100vh;
            margin: 30px 0;
        }

        .main {
            width: 500px;
            margin: 0 auto;
        }

        /* 表单 */
        .form {
            width: 100%;
            max-width: 500px;
            padding: 20px;
            background-color: #fff;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
        }

        .form-title {
            font-size: 24px;
            font-weight: bold;
            margin-bottom: 20px;
            text-align: center;
        }

        .form-item {
            margin-bottom: 15px;
        }

        .form-label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        .form-input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        .btn {
            display: inline-block;
            padding: 8px 16px;
            border: none;
            border-radius: 5px;
            text-decoration: none;
            color: #fff;
            background-color: #007bff;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #0056b3;
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
<div class="main">
    <form id="addForm" class="form" name="myform">
        <div class="form-title">修改密码</div>
        <div class="form-item flex-row">
            <%--@declare id="loginname"--%><label for="loginname" class="form-label">登录名</label>
            <label style="text-align: center; width: 80%" class="form-input">${sessionScope.admin.username}
            </label>
        </div>
        <div class="form-item">
            <label for="password" class="form-label">旧密码</label>
            <input type="password" class="form-input" name="password" id="password" placeholder="请输入密码" required
                   autocomplete="off">
        </div>
        <div class="form-item">
            <label for="newpassword" class="form-label">新密码</label>
            <input type="password" class="form-input" name="newpassword" id="newpassword" required placeholder="请输入新密码">
        </div>
        <div class="form-item">
            <label for="repassword" class="form-label">确认密码</label>
            <input type="password" class="form-input" name="repassword" id="repassword" required placeholder="请输入确认密码">
        </div>
        <div class="form-item">
            <button type="button" class="btn" id="sub">立即提交</button>
            <button type="reset" class="btn" id="res">取消重置</button>
        </div>
        <div id="btn-msg"></div>
    </form>
</div>

<script>
    document.getElementById('sub').onclick = function () {
        let userInfo = JSON.parse(localStorage.getItem('userInfo'));
        let pwd = document.getElementById('password').value;
        let newPwd = document.getElementById('newpassword').value;
        let repwd = document.getElementById('repassword').value;
        console.log(newPwd, repwd);
        if (newPwd !== repwd) {
            document.getElementById('btn-msg').innerHTML = "两次输入密码不一致！";
            setTimeout(()=>{
                document.getElementById('btn-msg').innerHTML = "";
            }, 2000);
            return
        }else {
            let editPwdDto = {
                id: userInfo.adminId,
                oldPassword: pwd,
                newPassword: newPwd,
                rePassword: repwd
            };
            axiosInstance.post(
                '/admin/editPwd',
                editPwdDto,
                {
                    headers: headersJson
                }
            ).then((res)=>{
                console.log(res.data);
                if (res.data.code == 200){
                    console.log("修改成功");
                    document.getElementById('btn-msg').innerHTML = res.data.desc;
                    setTimeout(()=>{
                        document.getElementById('btn-msg').innerHTML = "";
                        window.close();
                        window.open("/admin/loginPage");
                    }, 2000);
                }else {
                    console.log("修改失败")
                    document.getElementById('btn-msg').innerHTML = res.data.desc;
                    setTimeout(()=>{
                        document.getElementById('btn-msg').innerHTML = "";
                    }, 2000);
                }
            }).catch(e=>{
                console.log(e);
                console.log("修改出现异常");
                document.getElementById('btn-msg').innerHTML = "修改出现异常";
                setTimeout(()=>{
                    document.getElementById('btn-msg').innerHTML = "";
                }, 2000);
            })
        }
    }
</script>
</body>
</html>
